﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RVEAd.Business;
using RVEAd.Infrastructure;

namespace RVEAd.Forms
{
    public partial class FormAddEditCategory : Form, IAddEditForm
    {
        private RveBL rveBL = null;

        public bool IsEditMode { get; set; }
        public Business.Model.ClientCategory Category { get; set; }

        public FormAddEditCategory()
        {
            InitializeComponent();
            this.Load += new EventHandler(FormAddEditCategory_Load);
        }

        void FormAddEditCategory_Load(object sender, EventArgs e)
        {
            rveBL = new RveBL();

            if (IsEditMode)
            {
                CheckInput();
                PutBOinControlsValue();
            }
        }

        private void CheckInput()
        {
            if (Category == null)
            {
                throw new ArgumentNullException();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public bool ValidateInput()
        {
            if (string.IsNullOrEmpty(tbCategorie.Text))
            {
                MessageBox.Show("Introduceti o categorie", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        public void PutBOinControlsValue()
        {
            tbCategorie.Text = Category.Category;
        }

        public void PutControlsValueInBO()
        {
            Category.Category = tbCategorie.Text;
        }

        public void Add()
        {
            rveBL.AddCategory(tbCategorie.Text);
        }

        public void Edit()
        {
            PutBOinControlsValue();
            rveBL.UpdateCategory(Category);
        }

        public void Save()
        {
            if (Validate())
            {
                if (IsEditMode)
                {
                    Save();
                }
                else
                {
                    Add();
                }
            }

            this.Close();
        }
    }
}
